package com.yfp.client.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.client.domain.MealReplacement;
import com.yfp.client.mapper.MealReplacementMapper;
import com.yfp.client.service.MealReplacementService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author admin
 * @description 针对表【meal_replacement(代取餐状态表)】的数据库操作Service实现
 * @createDate 2024-05-24 19:03:57
 */
@Service
public class MealReplacementServiceImpl extends ServiceImpl<MealReplacementMapper, MealReplacement> implements MealReplacementService {

    @Resource
    private MealReplacementMapper mealReplacementMapper;


    /**
     * 根据设备id获取运营在线状态
     *
     * @param machineIds
     * @return
     */
    @Override
    public Map<String, Integer> operationalOnlineStatus(List<String> machineIds) {
        // 是否在线{0：否，1：在线}
        List<MealReplacement> mealReplacementDOS = this.listByMachineIds(machineIds);
        // 根据设备id与用户id分组，并且获取分组后按照时间排序的最后一条数据
        Map<String, Map<Long, MealReplacement>> collect = mealReplacementDOS.stream()
                .collect(Collectors.groupingBy(
                        MealReplacement::getMachineId,
                        Collectors.groupingBy(
                                MealReplacement::getUserId,
                                Collectors.collectingAndThen(
                                        Collectors.maxBy(Comparator.comparing(MealReplacement::getId)),
                                        optional -> optional.orElse(null)
                                )
                        )
                ));
        // 状态存在1则返回true否则false
        Map<String, Integer> map = collect.entrySet().stream().collect(Collectors.toMap(
                Map.Entry::getKey,
                entry -> entry.getValue().values().stream().map(MealReplacement::getStatus).anyMatch(status -> status.equals(1)) ? 1 : 0
        ));
        return map;
    }

    /**
     * 根据machineIds查询
     *
     * @param machineIds
     * @return
     */
    @Override
    public List<MealReplacement> listByMachineIds(List<String> machineIds) {
        if (CollUtil.isEmpty(machineIds)) {
            return Collections.emptyList();
        }

        LambdaQueryWrapper<MealReplacement> query = Wrappers.lambdaQuery(MealReplacement.class);
        query.in(MealReplacement::getMachineId, machineIds);
        return this.list(query);
    }

    /**
     * 根据用户id和设备id查询代取餐状态
     *
     * @param machineId 设备id
     * @return
     */
    @Override
    public List<MealReplacement> getByMachineId(String machineId) {
        LambdaQueryWrapper<MealReplacement> query = Wrappers.lambdaQuery(MealReplacement.class);
        query.eq(MealReplacement::getMachineId, machineId);

        return this.list(query);
    }

    @Override
    public Boolean getStatus(String machineId) {
        List<MealReplacement> mealReplacementDOS = this.getByMachineId(machineId);
        // 根据用户id分组，并且获取分组后按照时间排序的最后一条数据
        Map<Long, MealReplacement> collect = mealReplacementDOS.stream().collect(Collectors.toMap(MealReplacement::getUserId, Function.identity(), BinaryOperator.maxBy(Comparator.comparing(MealReplacement::getId))));
        // 状态存在1则返回true否则false
        return collect.values().stream().map(MealReplacement::getStatus).anyMatch(status -> status.equals(1));
    }
}




